{% extends "base.html" %}
{% load i18n dv_filters bootstrap3 %}
{% load salary_tags %}

{% block head_title %}{% blocktrans %}Décompte salaires {{ year }}{% endblocktrans %}{% endblock %}

{% block content %}
<nav>
  <ul class="pager">
    <li class="previous btn-xs">
      {% with previous_year=year|add:"-1" %}
        <a href="{% url 'salary:timesheets-overview' year=previous_year %}{% if canton %}?canton={{ canton }}{% endif %}" >
          <span aria-hidden="true">←</span>
          {{ previous_year }}
        </a>
      {% endwith %}
    </li>
    <li>
      <h1 class="h3" style="display: inline">{% blocktrans %}Décompte salaires {{ year }}{% endblocktrans %}</h1>
    </li>
    <li class="next btn-xs">
      {% with next_year=year|add:"1" %}
        <a href="{% url "salary:timesheets-overview" year=next_year %}{% if canton %}?canton={{ canton }}{% endif %}">
          {{ next_year }}
          <span aria-hidden="true">→</span>
        </a>
      {% endwith %}
    </li>
  </ul>
</nav>

<div class="center-block text-center timesheets-canton-chooser">
  <div class="btn-group" role="group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      {% if active_canton %}
        {% blocktrans %}Canton sélectionné: {{ active_canton }}{% endblocktrans %}
      {% else %}
        {% trans "Filtrer par canton" %}
      {% endif %}
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
      {% url 'salary:timesheets-overview' year=year as current_url %}
      <li><a href="{{ current_url|remove_qs:"canton" }}">{% trans "Tous les cantons" %}</a></li>
      {% for canton_code, canton_label in cantons %}
        <li><a href="{% add_qs current_url|remove_qs:"canton" canton=canton_code %}">{{ canton_label }}</a></li>
      {% endfor %}
    </ul>
  </div>
</div>

{% if timesheets_status_matrix %}
  <table class="table table-bordered timesheets-overview-table">
    <thead>
      <tr>
        <th></th>
        {% for month in months.values %}
          <th class="text-center">{{ month|capfirst }}.</th>
        {% endfor %}
      </tr>
    </thead>

    <tbody>
      {% for user, statuses in timesheets_status_matrix.items %}
      <tr>
        <td>
          <a href="{% url 'user-detail' pk=user.pk %}{% if canton %}?returnUrl={% url 'salary:timesheets-overview' year=year %}%3Fcanton%3D{{ canton }}{% endif %}">
            {{ user.get_full_name }}
          </a>
        </td>
        {% for status in statuses %}
          <td
            {% if status %}data-status="{{ status }}"{% endif %}class="text-center status-cell{% if status %} {{ status|timesheet_status_css_class }}{% endif %}">
            {% if status %}
              <a href="{% url 'salary:user-timesheets' year=year month=forloop.counter pk=user.pk %}{% if canton %}?returnUrl={% url 'salary:timesheets-overview' year=year %}%3Fcanton%3D{{ canton }}{% endif %}">
                <span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
              </a>
            {% endif %}
          </td>
        {% endfor %}
      </tr>
      {% endfor %}
    </tbody>

    <tfoot>
      <tr>
        <th>{% trans "Total CHF" %}</th>
        {% for amount in timesheets_amount %}
          <th>
            {% if user|can:'timesheet_editor' %}
              {% for tmonth, timesheets in orphaned_timesheets.items %}
                  {% if forloop.parentloop.counter == forloop.counter %}
                    {% if timesheets|length > 0 %}
                      <a
                        title="{% blocktrans count count=timesheets|length %}1 entrée orpheline{% plural %}{{ count }} entrées orphelines{% endblocktrans %}"
                        class="btn btn-danger"
                        href="{% url "salary:cleanup-timesheets" year=year month=forloop.counter %}{% if canton %}?canton={{ canton }}{% endif %}">
                        <span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>
                      </a>
                    {% else %}
                      {{ amount }}
                    {% endif %}
                  {% endif %}
              {% endfor %}
            {% else %}
              {{ amount }}
            {% endif %}
          </th>
        {% endfor %}
      </tr>
      {% if user|can:'timesheet_editor' %}
        <tr>
          <th>{% trans "Envoyer un rappel" %}</th>
          {% for show_reminder_button in show_reminder_button_months %}
            <th class="text-center">
              <a
                class="btn btn-info"
                title="{% trans "Envoyer un rappel aux collabora·teur·trices qui n’ont pas rempli leurs heures" %}"
                {% if show_reminder_button %}
                  href="{% url "salary:send-timesheets-reminder" year=year month=forloop.counter %}"
                {% else %}
                  disabled
                  aria-disabled="true"
                {% endif %}
              >
                <span class=" glyphicon glyphicon-envelope" aria-hidden="true"></span>
              </a>
            </th>
          {% endfor %}
        </tr>
      {% endif %}
      {% if user|can:'challenge_invoice_cru' %}
        <tr class="accounting-export">
          <th></th>
          {% for amount in timesheets_amount %}
            <th class="text-center">
              {% with "csv" as format %}
                <a
                  title="{% blocktrans %}Télécharger au format {{ format }}{% endblocktrans %}"
                  class="btn btn-info hide"
                  href="{% url "salary:accounting-export" year=year month=forloop.counter format=format %}{% if canton %}?canton={{ canton }}{% endif %}">
                  <span class=" glyphicon glyphicon-floppy-save" aria-hidden="true"></span>
                </a>
              {% endwith %}
            </th>
          {% endfor %}
        </tr>
      {% endif %}
      {% if user|can:'timesheet_control' %}
        <tr>
          <th>{% trans "Exports de contrôle" %}</th>
          {% for month in months %}
            <th class="text-center">
              {% with "xls" as format %}
                <a
                  title="{% blocktrans %}Télécharger au format {{ format }}{% endblocktrans %}"
                  class="btn btn-info"
                  href="{% url "salary:control-export" year=year month=forloop.counter format=format %}{% if canton %}?canton={{ canton }}{% endif %}">
                  <span class=" glyphicon glyphicon-floppy-save" ></span>
                </a>
              {% endwith %}
            </th>
          {% endfor %}
        </tr>
      {% endif %}
    </tfoot>
  </table>
{% else %}
  <div class="text-center">
    {% trans "Aucun résultat." %}
  </div>
{% endif %}
<script type="text/javascript">
  document.addEventListener("DOMContentLoaded", function (event) {
    $('table.timesheets-overview-table > thead > tr > th').each(function (index) {
      let shouldDisplayExport = 0;
      let shouldWaitDisplayExport = 0;
      $('table.timesheets-overview-table > tbody > tr').each(function () {
          const td = $(this).children('td').eq(index);
          if (td.data("status") === 4) {
            shouldDisplayExport += 1
          } else if (td.data("status")) {
            shouldWaitDisplayExport += 1
          }
      });
      if (shouldWaitDisplayExport > 0 || shouldDisplayExport === 0){
        $('table.timesheets-overview-table > tfoot > tr.accounting-export > th').eq(index).children("a").hide()
      }
      if (shouldWaitDisplayExport === 0 && shouldDisplayExport > 0){
        $('table.timesheets-overview-table > tfoot > tr.accounting-export > th').eq(index).children("a").removeClass("hide")
      }
    });
  });
</script>
{% endblock %}

